<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin/index' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>菜品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>第2步：填写基本信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="菜品" prop="dishesId">
        <el-select v-model="ruleForm.dishesId" placeholder="请选择">
          <el-option
              v-for="item in dishesListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="菜品名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>-->
      <el-form-item label="调味" prop="sauce">
        <el-input v-model="ruleForm.sauce"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="销量" prop="sales">
        <el-input v-model="ruleForm.sales"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="库存" prop="stock">
            <el-input v-model="ruleForm.stock"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库存阈警值" prop="stockThreshold">
            <el-input v-model="ruleForm.stockThreshold"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="关键词" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="backToPreStep()">上一步</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dishesListOptions: [
        {id: 1, name: '模拟菜品数据1'},
        {id: 2, name: '模拟菜品数据2'},
        {id: 3, name: '模拟菜品数据3'}
      ],
      ruleForm: {
        categoryId: '',
        categoryName: '',
        dishesId: '',
        dishesName: '',
        /*name: '红烧肉',*/
        price: '19',
        sauce: '微辣',
        sales: 50,
        stock: '5000',
        stockThreshold: '500',
        keywords: '肉类',
        sort: '95'
      },
      rules: {
        dishesId: [
          {type: 'number', required: true, message: '请选择菜品', trigger: 'blur'},
        ],
        /*name: [
          {required: true, message: '请输入菜品名称', trigger: 'blur'},
        ],*/
        sauce: [
          {required: true, message: '请输入调味', trigger: 'blur'},
        ],
        price: [
          {required: true, message: '请输入价格', trigger: 'blur'},
        ],
        stock: [
          {required: true, message: '请输入库存', trigger: 'blur'},
        ],
        stockThreshold: [
          {required: true, message: '请输入库存阈警值', trigger: 'blur'},
        ],
        keywords: [
          {required: true, message: '请输入关键词', trigger: 'blur'},
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    backToPreStep() {
      this.$router.push('add-new-1');
    },
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      if (!localRuleFormString) {
        this.$router.push('add-new-1');
      }

      let localRuleForm = JSON.parse(localRuleFormString);
      if (!localRuleForm.categoryId) {
        this.$router.push('add-new-1');
      }

      this.ruleForm.categoryId = localRuleForm.categoryId;
      this.ruleForm.categoryName = localRuleForm.categoryName;
    },
    loadDishesList() {
      let url = 'http://localhost:9989/dishess';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.dishesListOptions = responseBody.data;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 请求查询选择的菜品，判断是否存在
          let url = 'http://localhost:9989/dishess/' + this.ruleForm.dishesId;
          console.log('url = ' + url);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              // 将所有数据存储到localStorage中
              this.ruleForm.dishesName = responseBody.data.name;
              localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
              let xxx = localStorage.getItem('localRuleForm');
              let xxxObject = JSON.parse(xxx);
              console.log(xxxObject);
              // 跳转到下个视图
              this.$router.push('add-new-3');
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.loadLocalRuleForm();
    this.loadDishesList();
  }
}
</script>